import MathTool from "../../lib/MathTool";
import ItemBaseData from "../data/ItemBaseData";


export default class Item extends Laya.Sprite{
    private mIcon:Laya.Image;
    private mSelectImg:Laya.Image;
    private mOverImg:Laya.Image;
    private mrow:number;
    private mcol:number;
    private mSelect:boolean;
    private mdata:ItemBaseData;
    public isInGrid:boolean;
    private moff:number;
    private mPath=[];
    /**发射出来的*/
    public isFire:boolean;
    constructor(_data:ItemBaseData=null,_updatePos:boolean=true){
        super();
        this.mIcon=new Laya.Image();
        this.mIcon.width=this.mIcon.height=ItemBaseData.WIDTH;
        this.moff=ItemBaseData.WIDTH/2;
        this.mIcon.pos(-this.moff,-this.moff);
        this.addChild(this.mIcon);
        if(_data)this.setData(_data,_updatePos);
    }

    public setData(_data:ItemBaseData,_updatePos:boolean=true){
        this.mdata=_data;
        if(_data){
            if(_updatePos)this.updatePos();
            this.mIcon.skin=this.data.icon;
        }
        let tp = this.getIconPos();
        this.mIcon.pos(tp.x,tp.y);
        this.isFire=false;
        this.select=false;
        this.freshOverImg();
    }

    public changeDataToId(_id:number){
        this.data.setDataById(_id);
        this.mIcon.skin=this.data.icon;
    }

    public setRC(_row:number,_col:number,_tw:boolean=false,_func:Function=null,_time:number=0){
        this.mdata.setRC(_row,_col);
        this.updatePos(_tw,_func,_time);
    }

    public updatePos(_tw:boolean=false,_func:Function=null,_time:number=0){
        let tp = ItemBaseData.getPosByRC(this.mdata.row,this.mdata.col);
       
        if(_tw){
            Laya.Tween.to(this,{x:tp.x,y:tp.y},_time,null,Laya.Handler.create(this,()=>{
                if(_func)_func();
            }));
        }else{
            this.setPos(tp.x,tp.y);
            if(_func)_func();
        }
    }

    public moveByPath(_path,_row:number,_col:number,_func:Function=null,_prossFunc:Function=null){
        this.mPath=_path;
        //console.log("moveByPath",..._path);
        this.movePos(_func,_prossFunc);
    }

    public movePos(_func:Function=null,_prossFunc:Function=null,_time:number=0){
        let tp=this.mPath.shift();
        if(tp){
            let tease = this.mPath.length>0?null:Laya.Ease.bounceOut;
            let tdist = MathTool.getTwoPointDist(this.x,this.y,tp.x,tp.y);
            if(tdist<10){
                this.setPos(tp.x,tp.y);
                if(_prossFunc)_prossFunc(tp);
                if(this.mPath.length)this.movePos(_func,_prossFunc);
                else {
                    if(_func)_func();
                }
            }else{
                let time = tdist;
                Laya.Tween.to(this,{x:tp.x,y:tp.y,update:new Laya.Handler(this,()=>{
                    if(_prossFunc)_prossFunc({x:this.x,y:this.y});
                })},time,tease,Laya.Handler.create(this,()=>{
                    if(_prossFunc)_prossFunc(tp);
                    if(this.mPath.length)this.movePos(_func,_prossFunc);
                    else {
                        if(_func)_func();
                    }
                }))     
            }
        }else{
            if(_func)_func(); 
        }
    }

    public getDiamandPrice(){
        let tvalue:number=0;
        return tvalue;
    }

    public setPos(_x:number,_y:number){
        this.pos(_x,_y);
    }

    public get data():ItemBaseData{
        return this.mdata;
    }

    public get select(){
        return this.mSelect;
    }

    public set select(_value:boolean){
        this.mSelect=_value;
        this.freshSelect();
    }

    public getStr(){
        return this.mdata.getStr();
    }

    private freshSelect(){
        if(this.mSelect){
            if(!this.mSelectImg){
                this.mSelectImg=new Laya.Image("main/fire_ring.png");
                this.mSelectImg.anchorX=this.mSelectImg.anchorY=0.5;
                this.addChild(this.mSelectImg);
            }
            this.mSelectImg.visible=true;
        }else{
            if(this.mSelectImg)this.mSelectImg.visible=false;
        }
    }

    private freshOverImg(){
        if(this.data.isDiamandType){
            if(!this.mOverImg){
                this.mOverImg=new Laya.Image();
                this.mOverImg.anchorX=this.mOverImg.anchorY=0.5;
                this.addChild(this.mOverImg);
            }
            this.mOverImg.visible=true;
            this.mOverImg.skin=this.getOverSkin();
            this.mOverImg.scaleY=this.getOverSclaeY();
            let ttp=this.getOverPos();
            this.mOverImg.pos(ttp.x,ttp.y);
        }else{
            if(this.mOverImg)this.mOverImg.visible=false;
        }
    }

    private getOverSkin(){
        let tskin="";
        if(this.data.isColorDiamand){
            tskin="items/zzz"+this.data.colorIndex+"_1.png";
        }else if(this.data.isDiamand){
            tskin="items/zzz6_"+this.data.index+".png";
        }
        return tskin;
    }

    private getOverPos(){
        let tp={x:0,y:0};
        if(this.data.isDiamand){
            if(this.data.index==2){
                // tp.x=ItemBaseData.WIDTH/2; 
                 tp.y=ItemBaseData.WIDTH/2;
            }else if(this.data.index==3){
                //tp.x=ItemBaseData.WIDTH/2; 
                tp.y=-ItemBaseData.WIDTH/2;
            }else if(this.data.index==4){
                tp.y=ItemBaseData.WIDTH;
            }
            else if(this.data.index==5){
                //tp.x=ItemBaseData.WIDTH; 
                tp.y=-ItemBaseData.WIDTH;
            }
        }
        return tp;
    }

    private getIconPos(){
        let tp={x:-this.moff,y:-this.moff};
        if(this.data.isDiamand){
            if(this.data.index==2){
                // tp.x=ItemBaseData.WIDTH/2; 
                 tp.y += ItemBaseData.WIDTH/2;
            }else if(this.data.index==3){
                //tp.x=ItemBaseData.WIDTH/2; 
                tp.y += -ItemBaseData.WIDTH/2;
            }else if(this.data.index==4){
                tp.y += ItemBaseData.WIDTH;
            }
            else if(this.data.index==5){
                //tp.x=ItemBaseData.WIDTH; 
                tp.y += -ItemBaseData.WIDTH;
            }
        }
        return tp;
    }

    private getOverSclaeY(){
        let tscle=1;
        if(this.data.index==3 || this.data.index==5)tscle=-1;
        return tscle;
    }

}